const {contextBridge, ipcRenderer} = require('electron');

const ipc = {
    'render': {
        'sendReceive': [
            'db:getQuesList',
            'db:getQuesCount',
            'db:getQuesTypes',
            'db:getQuesRatingRanges',
            'db:getRandomIdList',
            'ques:getSelected',
            'ques:generatePdf',
            'history:getList',
            'history:getCount',
            'star:getList',
            'star:getCount',
            'star:removeItem',
            'star:addItem',
        ],
        'send': [],
        'receive': [],
    }
};

contextBridge.exposeInMainWorld('ipcRender', {
        invoke: (channel, args) => {
            let validChannels = ipc.render.sendReceive;
            if (validChannels.includes(channel)) {
                return ipcRenderer.invoke(channel, args);
            }
        },
        send: (channel, args) => {
            let validChannels = ipc.render.send;
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, args);
            }
        },
        receive: (channel, listener) => {
            let validChannels = ipc.render.receive;
            if (validChannels.includes(channel)) {
                ipcRenderer.on(channel, (event, ...args) => listener(...args));
            }
        }
    }
);
